<?php
if(!defined('InDesign')) exit('Access Denied');
define('InDesignRoot', substr(dirname(__FILE__), 0, -4));
define('InDesignStyle', 'view/style/');
error_reporting(E_ALL ^ E_NOTICE);

$lang=array(
    'main/title'=>'PHP Designer In Trunk'
);

require InDesignRoot.'main/common.h.php';
include InDesignRoot.'config/main.php';
require InDesignRoot.'main/error.php';

MVCROOT::loadModel('develop/debug');


class MVCROOT {
    static $Model=array();
    static $Class=array();
    static $ViewSpace=array();
    static $Current_Controller;
    function loadModel() {
        $args=func_get_args();
        foreach($args as $arg){
            if(!isset(self::$Model[$arg])){
                require InDesignRoot.'model/'.$arg.'.php';
                self::$Model[$arg]=1;
               // echo 'Load Model '.$arg.' Successfully.<br/>';
            }
        }
    }
    function appendClass($arg, $class) {
        self::$Class[$arg]=$class;
        // MVCROOT::appendModel('develop/debug', new Debugger());
    }
    function loadView($viewname) {
        global $lang;
        require InDesignRoot.'view/main/'.$viewname.'.php';
    }
    function loadChildView($viewname) {
        global $lang;
        require InDesignRoot.'view/child/'.$viewname.'.php';
    }
    function setController($controller) {
        self::$Current_Controller=$controller;
        $controllers=(array)explode('/', $controller);
        $final=array_pop($controllers);

        $ControllerEntry=InDesignRoot.'controller/';

        foreach($controllers as $level){
            require $ControllerEntry.$level.'/base.php';
            $ControllerEntry=$ControllerEntry.$level.'/';
        }

        require $ControllerEntry.$final.'.php';

        // setController('user/protector/sql-check/checkprogress');
        /*
            load controller/user/base.php
            load controller/user/protector/base.php
            load controller/user/protector/sql-check/base.php
            load controller/user/protector/sql-check/checkprogress.php
        */
    }
}
class MVCFNC {
    static $Hook=array();
    function load($name, $args=array()) {
        $result=array();
        if(isset(self::$Hook[$name]) && !empty(self::$Hook[$name])){
            foreach(self::$Hook[$name] as $func){
                $result[]=call_user_func($func, $args);
            }
        }
        return $result;
    }
    function add($name, $func) {
        if(!isset(self::$Hook[$name])) self::$Hook[$name]=array();
        self::$Hook[$name][]=$func;
        return true;
    }
    function remove($name, $func) {
        if(!isset(self::$Hook[$name])) return false;
        self::$Hook[$name]=array_diff(self::$Hook[$name], array($func));
        return true;
    }
}
class MVCHK {
    public static $Extended;
    public function addListener($ECName, $func, $listener, $pre=0) {
        if($pre==1){
            self::$Extended[$ECName][$func][0][]=$listener;
        }else{
            self::$Extended[$ECName][$func][2][]=$listener;
        }
        return true;
    }
    public function addHook($ECName, $func, $inline) {
        self::$Extended[$ECName][$func][1][]=$inline;
        return true;
    }
}
class MVCECBase {
    public $ExtendedFunctions;
    public $ECName;
     private function doExtended($func, $args) {
        $ls=MVCHK::$Extended[$this->ECName];
        if($ws=$ls[$func][0]){
            foreach($ws as $act){
                call_user_func($act, $args);
            }
        }
        if($ws=$ls[$func][1]){
            foreach($ws as $act){
                $callback=call_user_func($act, $args);
                if($callback[0]==INLN_SKIP){
                    return array(XTND_SKIP, array());
                }elseif($callback[0]==INLN_RWRT){
                    $args=$callback[1];
                }
            }
        }
        if($ws=$ls[$func][2]){
            foreach($ws as $act){
                call_user_func($act, $args);
            }
        }
        return array(XTND_ARGS, $args);
    }
    public function __call($name, $args) {
        if(in_array($name, $this->ExtendedFunctions)){
            $status=$this->doExtended($name, $args);
            if($status[0]==XTND_SKIP) return false;
            elseif($status[0]==XTND_ARGS) $args=$status[1];
        }
        return call_user_func(array($this, '_'.$name), $args);
    }
}


function loadDataBase() {
    global $db;
    static $load=0;

    if($load==1) return true;

    if($db['main']){
        require InDesignRoot.'main/db.php';

        db::Initialize($db['main']);
    }

    $load=1;
    return true;
}